package com.huisou.dao;

import com.huisou.Constants;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class SaleDao {
	
	private static final Logger	log	= LoggerFactory.getLogger(SaleDao.class);
	
	private JdbcTemplate		jt;
	
	@Resource(name = "dataSource")
	public void setDs(DataSource ds) {
		jt = new JdbcTemplate(ds);
	}
	
	/**
	 * 查询已删除的产品ID列表
	 * 
	 * @param lastUpdate
	 * @return
	 */
	public List<String> queryDeleted(String lastUpdate) {
		List<String> ls = new ArrayList<String>();
		try {
			ls = jt.query("select id  from e_product_sale where status = ? and modified >= ?", new Object[] { Constants.DELETE, lastUpdate}, new RowMapper<String>() {
				public String mapRow(ResultSet rs, int rowNum) throws SQLException {
					return "1t" + rs.getString("id");
				}
			});
		}
		catch (DataAccessException e) {
			log.error("获取已删除出错e_product_sale.");
			return null;
		}
		return ls;
	}
	
	/**
	 * 查询已删除的公司ID列表
	 * 
	 * @param lastUpdate
	 * @return
	 */
	public List<String> queryCompanyDeleted(String lastUpdate) {
		List<String> ls = new ArrayList<String>();
		try {
			ls = jt.query("select id  from tbl_enterprise where status = ? and modify_time >= ? ", new Object[] { Constants.DELETE, lastUpdate }, new RowMapper<String>() {
				public String mapRow(ResultSet rs, int rowNum) throws SQLException {
					return rs.getString("id");
				}
			});
		}
		catch (DataAccessException e) {
			log.error("获取已删除出错e_enterprise.");
			return null;
		}
		return ls;
	}
	
	/**
	 * 查询已删除的资讯ID列表
	 * 
	 * @param lastUpdate
	 * @return
	 */
	public List<String> queryNewsDeleted(String lastUpdate) {
		List<String> ls = new ArrayList<String>();
		try {
			ls = jt.query("select id  from hc_info where status = ? and updatedate >= ? ", new Object[] { Constants.DELETE, lastUpdate }, new RowMapper<String>() {
				public String mapRow(ResultSet rs, int rowNum) throws SQLException {
					return rs.getString("id");
				}
			});
		}
		catch (DataAccessException e) {
			log.error("获取已删除出错hc_info.");
			return null;
		}
		return ls;
	}
}
